package cqrtplm.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ty.basic.response.ResponseResult;
import cqrtplm.constant.RouteConstant;
import cqrtplm.entity.RtComSupplierDO;
import cqrtplm.service.RtComSupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "公共-供应商信息")
@RequestMapping("/rest"+RouteConstant.RATO + "supplier/" + "v1")
public class RtComSupplierController {

    @Resource
    private RtComSupplierService rtComSupplierService;

    @PostMapping("/list")
    @ApiOperation("获取供应商列表")
    public ResponseResult list(@RequestBody RtComSupplierDO rtComSupplier) {
        List<RtComSupplierDO> list = rtComSupplierService.selectRtComSupplierList(rtComSupplier);
        return ResponseResult.success(list);
    }

    @PostMapping("/pagelist")
    @ApiOperation("分页获取供应商列表")
    public ResponseResult<PageInfo<RtComSupplierDO>> pagelist(
            @RequestBody RtComSupplierDO rtComSupplier,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int pageSize) {
        PageHelper.startPage(page, pageSize);
        List<RtComSupplierDO> list = rtComSupplierService.selectRtComSupplierList(rtComSupplier);
        return ResponseResult.success(new PageInfo<>(list));
    }

    @GetMapping("/{id}")
    @ApiOperation("获取供应商详情")
    public ResponseResult getById(@PathVariable String id) {
        return ResponseResult.success(rtComSupplierService.selectRtComSupplierById(id));
    }

    @GetMapping("/code/{supplierCode}")
    @ApiOperation("根据供应商编码获取详情")
    public ResponseResult getByCode(@PathVariable String supplierCode) {
        return ResponseResult.success(rtComSupplierService.selectRtComSupplierByCode(supplierCode));
    }

    @PostMapping("/add")
    @ApiOperation("新增供应商")
    public ResponseResult add(@RequestBody RtComSupplierDO rtComSupplier) {
        return ResponseResult.success(rtComSupplierService.insertRtComSupplier(rtComSupplier));
    }

    @PostMapping("/edit")
    @ApiOperation("修改供应商")
    public ResponseResult edit(@RequestBody RtComSupplierDO rtComSupplier) {
        return ResponseResult.success(rtComSupplierService.updateRtComSupplier(rtComSupplier));
    }

    @DeleteMapping("/del")
    @ApiOperation("删除供应商")
    public ResponseResult remove(@RequestBody List<String> ids) {
        return ResponseResult.success(rtComSupplierService.deleteRtComSupplierByIds(ids));
    }

    @GetMapping("/queryComSupplier")
    @ApiOperation("模糊匹配供应商")
    public ResponseResult<List<JSONObject>> queryComSupplier(String queryCode) {
        List<JSONObject> result = new ArrayList<>();
        List<RtComSupplierDO> list =null;
        RtComSupplierDO rtComSupplier = new RtComSupplierDO();
        if (StringUtils.isNotBlank(queryCode)){
            rtComSupplier.setSupplierCode(queryCode);
            rtComSupplier.setFullName(queryCode);
            rtComSupplier.setShortName(queryCode);
            rtComSupplier.setState("1");
            list = rtComSupplierService.selectRtComSupplierList(rtComSupplier);
        }else{
            rtComSupplier.setState("1");
            list = rtComSupplierService.selectRtComSupplierList(rtComSupplier);
        }
        if (CollectionUtils.isNotEmpty(list)){
            for (RtComSupplierDO supplierDO : list){
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id",supplierDO.getSupplierCode());
                jsonObject.put("name",supplierDO.getShortName());
                result.add(jsonObject);
            }
        }
        return ResponseResult.success(result);
    }
}